package com.sunnyweather.android.ui.place

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.sunnyweather.android.MainActivity
import com.sunnyweather.android.R
import com.sunnyweather.android.databinding.PlaceItemLayoutBinding
import com.sunnyweather.android.logic.model.Place
import com.sunnyweather.android.logic.util.LogUtil
import com.sunnyweather.android.ui.weather.WeatherActivity

class PlaceAdapter(val fragment:PlaceSearchFragment,val placeList:List<Place>):RecyclerView.Adapter<PlaceAdapter.ViewHolder>() {

    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val placeItemLayoutBinding=PlaceItemLayoutBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(fragment.context).inflate(R.layout.place_item_layout,parent,false)
        val holder=ViewHolder(view)
        val activity=fragment.activity
        holder.itemView.setOnClickListener {
            val position=holder.adapterPosition
            val place=placeList[position]
            if(activity is WeatherActivity){
                activity.weatherViewModel.apply {
                    lat=place.location.lat
                    lng=place.location.lng
                    placeName=place.name
                    refreshWeather()
                }
                //fragment.placeViewModel.addPlace(place)
                //清空搜索栏并关闭侧拉菜单
                fragment.searchPlaceFragmentLayoutBinding.searchPlaceEditText.text.clear()
                activity.weatherLayoutBinding.drawerLayout.closeDrawer(GravityCompat.START)
            }else{
                val intent= Intent(fragment.context,WeatherActivity::class.java).apply {
                    putExtra("place_lat",place.location.lat)
                    putExtra("place_lng",place.location.lng)
                    putExtra("place_name",place.name)
                }
                fragment.startActivity(intent)
                activity?.finish()
            }

            //保存选择的地点
            fragment.placeViewModel.setPlace(place)
        }

        holder.placeItemLayoutBinding.addPlaceBtn.setOnClickListener {
            val position=holder.adapterPosition
            val place=placeList[position]
            LogUtil.d("addPlaceBtn",place.toString())
            if(activity is WeatherActivity){
                if(fragment.placeViewModel.getPlaceIndex()<3){
                    if(activity.weatherViewModel.placeOptions.contains(place)){
                        Toast.makeText(fragment.context,"当前地点已存在",Toast.LENGTH_SHORT).show()
                    }else{
                        fragment.placeViewModel.addPlace(place)
                        activity.weatherViewModel.getPlaceOptions()
                        fragment.searchPlaceFragmentLayoutBinding.searchPlaceEditText.text.clear()
                        activity.weatherLayoutBinding.drawerLayout.closeDrawer(GravityCompat.START)
                    }
                }else{
                    Toast.makeText(fragment.context,"你最多只能添加三个备用地点",Toast.LENGTH_SHORT).show()
                }
            }else{
                Toast.makeText(fragment.context,"请先选择一个默认地点再添加备用地点",Toast.LENGTH_SHORT).show()
            }
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val place=placeList[position]
        holder.placeItemLayoutBinding.placeName.text=place.name
        holder.placeItemLayoutBinding.placeAddress.text=place.address
    }

    override fun getItemCount(): Int {
        return placeList.size
    }
}